package com.xwj.controller;

import com.xwj.common.dto.BaseResponse;
import com.xwj.dto.UserAccountDto;
import com.xwj.service.RedisLockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 使用redis分布式锁实现银行账户提现高并发测试
 *
 * @author xwj
 * @date 2020/4/29
 */
@RestController
@RequestMapping("/redis/lock/")
public class RedisLockController {
    @Autowired
    private RedisLockService redisLockService;

    /**
     * 使用SET K V NX PX操作实现redis分布式锁
     * @param dto
     * @return
     */
    @GetMapping("/takeMoneyRedisLock")
    public BaseResponse takeMoneyRedisLock(@Validated UserAccountDto dto) {
        try {
            redisLockService.takeMoneyRedisLock(dto);
            return BaseResponse.success();
        } catch (Exception e) {
            return BaseResponse.error(e.getMessage());
        }
    }

}
